package com.mldong.context.constant;

import cn.hutool.core.convert.Convert;
import com.mldong.exception.ServiceException;

public class ConstantContextHolder {
    public static String getImgBaseUrl(){
        return getImgBaseUrl(false);
    }
    public static String getImgBaseUrl(boolean isNullThrow){
        return getSysConfig("IMG_BASE_URL",String.class,isNullThrow);
    }

    /**
     * 获取系统参数
     * @param code
     * @param clazz
     * @param defaultValue
     * @param <T>
     * @return
     */
    public static <T> T getSysConfigWithDefault(String code,Class<T> clazz,T defaultValue){
        return Convert.convert(clazz,ConstantContext.me().getObj(code),defaultValue);
    }
    /**
     * 获取系统参数
     * @param code
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getSysConfig(String code,Class<T> clazz){
        return getSysConfig(code, clazz, false);
    }
    /**
     * 获取系统参数
     * @param code
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getSysConfig(String code,Class<T> clazz, boolean isNullThrow){
        Object object = ConstantContext.me().getObj(code);
        if(object == null && isNullThrow){
            ServiceException.throwBiz(99999999,code+"参数未配置");
        }
        return Convert.convert(clazz,object);
    }
}
